package com.tianyi.sfzy1.controller;

import com.github.pagehelper.PageInfo;
import com.tianyi.sfzy1.domain.Course;
import com.tianyi.sfzy1.service.CourseService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * DYT
 */

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     *查询所有课程
     */
    @GetMapping
    public R findAll(String courseName,String consultantName,String courseType,int pageNum,int pageSize){
        System.out.println(courseName);
        System.out.println(consultantName);
        System.out.println(courseType);
        PageInfo<Course> pageInfo=courseService.findAll(courseName,consultantName,courseType,pageNum,pageSize);
        return R.ok(pageInfo);
    }

    /**
     * 咨询师ID查课程
     */
    @GetMapping("/id")
    public R findById(int consultantId){
        return R.ok(courseService.findById(consultantId));
    }

    /**
     *添加新课程
     */
    @PostMapping
    public R insert(@RequestBody Course course){
        int i=courseService.insert(course);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("添加失败");
        }
    }

    /**
     *修改课程
     */
    @PutMapping
    public R update(@RequestBody Course course){
        int i=courseService.update(course);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("修改失败");
        }
    }

    /**
     *删除课程
     */
    @DeleteMapping
    public R delete(int courseId){
        int i=courseService.delete(courseId);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("删除失败");
        }
    }

    /**
     *批量删除课程
     */
    @DeleteMapping("/select")
    public R deleteSelect(@RequestBody List<Integer> list){
        int i=courseService.deleteSelect(list);
        if (i>0){
            return R.ok(null);
        }
        else {
            return R.fail("删除失败");
        }
    }
    //   查询课程表购买记录表 已购买
    @GetMapping("/findCourseBuyed")
    public R findCourseBuy(){
        return R.ok(courseService.findCourseAndPayed());
    }
    //   查询课程表购买记录表 未购买
    @GetMapping("/findCourseNotBuyed")
    public R findCourseNotBuy(){
        return R.ok(courseService.findCourseNotPayed());
    }

}
